package com.crm.service.impl.index;

import com.crm.dao.mapper.sys.NewsMapper;
import com.crm.dao.mapper.sys.NotifyMapper;
import com.crm.model.entity.sys.News;
import com.crm.model.entity.sys.NewsExample;
import com.crm.model.entity.sys.Notify;
import com.crm.model.entity.sys.NotifyExample;
import com.crm.service.index.NewsDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsDbServiceImpl implements NewsDbService {

    @Autowired
    NewsMapper newsMapper;
    @Autowired
    NotifyMapper notifyMapper;


    @Override
    public List<News> getImgList() {
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria =  newsExample.createCriteria();
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
        newsExample.setOrderByClause("news_order asc");
        List<News> news = newsMapper.selectByExampleWithBLOBs(newsExample);
        return news;
    }

    @Override
    public Boolean updateNewInfo(News news) {
        int num =newsMapper.updateByPrimaryKeySelective(news);
        if(num>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean addNesInfo(News news) {
        int num =newsMapper.insertSelective(news);
        if(num>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public News getNewsInfoByNewOrder(Integer order) {
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        criteria.andNewsOrderEqualTo(order);
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
        List<News> news = newsMapper.selectByExampleWithBLOBs(newsExample);
        if(news.size()>0){
            return news.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List<Notify> selectNotifyBySearchInfo(String searchInfo,Integer publicStatus,String sort,String sortOrder) {
        NotifyExample notifyExample = new NotifyExample();
        NotifyExample.Criteria criteria = notifyExample.createCriteria();
        criteria.andTypeEqualTo(2);
        if (StringUtils.isNotEmpty(searchInfo)) {
            criteria.andTitleLike("%" + searchInfo.trim() + "%");
        }
        if (null == publicStatus || publicStatus == 0) {//已发布
            criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
        } else {//已删除
            criteria.andDeleteStatusEqualTo(Short.parseShort("1"));
        }
        //拼凑表格排序内容
        String order = "is_top desc";
        if(!StringUtils.isBlank(sort)&&!StringUtils.isBlank(sortOrder)){
            switch(sort){
                case "createTm":
                {
                    order= order + ",create_tm " + sortOrder;
                }
                break;
                case "updateTm":
                {
                    order= order + ",update_tm " + sortOrder;
                }
                break;
                default:
                    break;
            }

        }
        notifyExample.setOrderByClause(order);
        List<Notify> notifies = notifyMapper.selectByExampleWithBLOBs(notifyExample);
        return notifies;
    }

    @Override
    public Boolean addNotify(Notify notify) {
        Boolean result = false;
        int num = notifyMapper.insertSelective(notify);
        if (num > 0) {
            result = true;
        }
        return result;
    }



    @Override
    public Boolean updateNotify(Notify notify) {
        Boolean result = false;
        int num = notifyMapper.updateByPrimaryKeySelective(notify);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public Notify selectNotifyById(String id) {
        Notify notify = notifyMapper.selectByPrimaryKey(id);
        return notify;
    }
}
